<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
  <head>
    <meta charset="UTF-8">
    <title>用户页面-Long-admin</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
    <link  th:href="@{/X-admin/css/font.css}" rel="stylesheet" type="text/css">
    <link  th:href="@{/X-admin/css/xadmin.css}" rel="stylesheet" type="text/css">
  </head>
  <body>
    <div class="x-nav">
      <input id="getValue" type="hidden"/>
      <span class="layui-breadcrumb">
        <a href="">首页</a>
        <a href="">UPMS</a>
        <a>
          <cite>用户</cite></a>
      </span>

      <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:30px">&#x1002;</i></a>
    </div>
    <div class="x-body">
      <div class="layui-row">
        <form class="layui-form layui-col-md12 x-so">
          <input class="layui-input" placeholder="开始日" name="createdatetimeStart" id="start">
          <input class="layui-input" placeholder="截止日" name="createdatetimeEnd" id="end">
          <input type="text" name="username"  placeholder="请输入用户名" autocomplete="off" class="layui-input">
          <button class="layui-btn"  lay-submit="" lay-filter="sreach"><i class="layui-icon">&#xe615;</i></button>
        </form>
      </div>
      <div sec:authorize="hasAnyRole('ROLE_USER_DELBATCH','ROLE_USER_ADD')" class="layui-btn-group demoTable">
        <button sec:authorize="hasRole('ROLE_USER_DELBATCH')" class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon"></i>批量删除</button>
        <button sec:authorize="hasRole('ROLE_USER_ADD')" class="layui-btn" onclick="x_admin_show('添加用户','/userController/addPage',600,400)"><i class="layui-icon"></i>添加</button>
        <button class="layui-btn" data-type="delete">删除</button>
        <button class="layui-btn" data-type="edit">修改</button>
        <button class="layui-btn" data-type="grant">授权</button>
        <button class="layui-btn" data-type="getCheckData">获取选中行数据</button>
        <button class="layui-btn" data-type="getCheckLength">获取选中数目</button>
        <button class="layui-btn" data-type="isAll">验证是否全选</button>
      </div>
      <table class="layui-table" id="userList" lay-filter="userList">

      </table>

    </div>

    <script type="text/javascript"  th:attr="src=@{/X-admin/lib/jquery-3.2.1.min.js}"></script>
    <script type="text/javascript"  th:attr="src=@{/X-admin/lib/layui/layui.js}"></script>
    <script type="text/javascript"  th:attr="src=@{/X-admin/js/xadmin.js}"></script>
    <script type="text/javascript"  th:attr="src=@{/js/hylUtil.js}"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

  </body>

</html>


<script>
    var tableIns;

    layui.use('laydate', function(){
        var laydate = layui.laydate;

        //执行一个laydate实例
        laydate.render({
            elem: '#start', //指定元素
            type:'datetime'
        });

        //执行一个laydate实例
        laydate.render({
            elem: '#end', //指定元素
            type:'datetime',
        });
    });

    layui.use(['table','jquery'], function(){
        var table = layui.table
        tableIns=table.render({
            elem: '#userList'
            ,url:'/userController/list'
            ,cellMinWidth: 80
            ,cols: [[
                {type:'numbers'}
                ,{type: 'checkbox'}
                ,{field:'id', title:'ID', width:100, unresize: true, sort: true}
                ,{field:'username', title:'用户名'}
                ,{field:'password', title:'密码',with:60}
                ,{field:'createdatetime', title: '创建时间', with:90,templet: '<div>{{ format(d.createdatetime,"yyyy-MM-dd hh:mm:ss")}}</div>'}
                ,{field:'modifydatetime', title: '修改时间', with:90,templet: '<div>{{ format(d.modifydatetime,"yyyy-MM-dd hh:mm:ss")}}</div>'}
//                ,{field:'roleIds', title:'角色id',with:60,style:'display:none'}
                ,{field:'roleNames', title:'角色名称',with:60}
            ]]
            ,even:true
            ,page: true
            ,limits: [5, 7, 10]
            ,limit: 5 //每页默认显示的数量
        });

        //监听表格复选框选择
        table.on('checkbox(userList)', function(obj){
            console.log(obj)
        });

        var $ = layui.$, active = {
            delete: function(){ //获取选中要删除的数据
                var checkStatus = table.checkStatus('userList')
                    ,data = checkStatus.data;

                if(data.length==1){
                    layer.confirm('真的删除行么', function(index){
                        layer.close(index);
                        //向服务端发送删除指令
                        $.ajax({
                            type: 'POST',
                            url: "/userController/delete",
                            data: {"id":data[0].id},
                            success: function (result) {
                                if (result.success){
                                    location.reload();
                                }else{
                                    layer.msg(result.msg,"错误提示");
                                }
                            },
                            dataType: "json"
                        });
                    });
                }else {
                    layer.msg('选中了：'+ data.length + ' 个'+" 请选中一行");
                }

            }
            ,edit: function(){ //获取选中数据
                var checkStatus = table.checkStatus('userList')
                    ,data = checkStatus.data;

                if (data.length==1){
                    //编辑
                    $("#getValue").data("obj", data);
                    x_admin_show('编辑用户','/userController/editPage',600,400);
                }else {
                    layer.msg('选中了：'+ data.length + ' 个'+" 请选中一行");
                }

            }
            ,grant: function(){ //获取选中数据
                var checkStatus = table.checkStatus('userList')
                    ,data = checkStatus.data;
                if (data.length>=1){
                    //编辑
                    $("#getValue").data("obj", data);
                    x_admin_show('用户授权','/userController/grantPage',300,500);
                }else {
                    layer.msg('选中了：'+ data.length + ' 个'+" 请选中一行或多行");
                }
            }
            ,getCheckData: function(){ //获取选中数据
                var checkStatus = table.checkStatus('userList')
                    ,data = checkStatus.data;
                layer.alert(JSON.stringify(data));
            }
            ,getCheckLength: function(){ //获取选中数目
                var checkStatus = table.checkStatus('userList')
                    ,data = checkStatus.data;
                layer.msg('选中了：'+ data.length + ' 个');
            }
            ,isAll: function(){ //验证是否全选
                var checkStatus = table.checkStatus('userList');
                layer.msg(checkStatus.isAll ? '全选': '未全选')
            }
        };

        $('.demoTable .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
<script type="text/javascript">
    layui.use('form', function(){
        var form = layui.form;

        //监听提交
        form.on('submit(sreach)', function(data){
            layer.msg(JSON.stringify(data.field));
            tableIns.reload({
                where:data.field
            });
            return false;
        });
    });
</script>